/**********************************************************************/
/*
Author: Karan Makkar, adapted from Michelle's code
Created: Nov 2023
Updated: Aug 2025, by Youssef Assarssah
Description: Master program to run and output SUS/SAK outcomes regressions
Output: Tables 5, A11b, A11c, A16, A19-A24
*/
/**********************************************************************/


/*----------------------------------------------------*/
* Section: Setup
/*----------------------------------------------------*/

* include filepaths 
if "$master_run" !="1" include "./Do/SET_FILEPATHS.do"

clear all
set more off
set matsize 11000

* Log
cap log close
global prefix: display %tdCYND td(`c(current_date)')
log using "$KP_logs/${prefix}_sus_sak_outcomes_pwin_master.txt", text replace

* Total or percapita consumptions
local pcap = 0

/*----------------------------------------------------*/
* Section: Run programs and set dataset filepaths
/*----------------------------------------------------*/

* Regression programs
include "$KP_do/3 Functions/impact_regression_programs"

* Table formatting and flipping programs
include "$KP_do/3 Functions/impact_table_programs"

python: 

import re
def multicol_tex_rows(input_file_path, output_file_path, f, l):
    with open(input_file_path, 'r') as file:
        lines = file.readlines()

    modified_lines = []
    row_counter = 2  # Initialize a counter to keep track of the row index within the table

    for line in lines:
        if '&' in line:  # Assuming rows of interest contain '&' as part of the tabular data
            row_counter += 1
            if row_counter >= f and row_counter <= l and (row_counter - f) % 4 == 0:
                # Replace numbers in the row as specified
                modified_line = re.sub(r'&\s*\(([0-9.]+)\)\s*&', r'& \\multicolumn{2}{c}{\1}', line)
                modified_lines.append(modified_line)
                continue
        modified_lines.append(line)

    with open(output_file_path, 'w') as file:
        file.writelines(modified_lines)
end

* Set filepaths for dataset
global aug20 "$KP_deid_sakernas/Clean/sak_aug20_deid_clean_merged.dta"
global feb21 "$KP_deid_sakernas/Clean/sak_feb21_deid_clean_merged.dta"
global aug21 "$KP_deid_sakernas/Clean/sak_aug21_deid_clean_merged.dta"
global sep20 "$KP_deid_susenas/Clean/sus_sep20_deid_clean_merged.dta"
global mar21 "$KP_deid_susenas/Clean/sus_mar21_deid_clean_merged.dta"
global sep21 "$KP_deid_susenas/Clean/sus_sep21_deid_clean_merged.dta"
global mar22 "$KP_deid_susenas/Clean/sus_mar22_deid_clean_merged.dta"

/*----------------------------------------------------*/
* Section: Outcome variable lists
/*----------------------------------------------------*/

* Table 1: Main Table Vars
if `pcap' ==0 {
    global main_vars totcons ///
                employed ///
                self_emp_bus_owner ///
                hours_worked ///
                hourly_adj_wage ///
                ind_adj_earnings ///
                looking_job ///
                prep_new_bus 

    global susonly totcons ///
                employed ///
                self_emp_bus_owner ///
                hours_worked ///

    local consvar totcons
    local pc_suff _tot
}

else {
    global main_vars tot_adj_cons ///
                employed ///
                self_emp_bus_owner ///
                hours_worked ///
                hourly_adj_wage ///
                hh_adj_earnings ///
                looking_job ///
                prep_bus_hh 

    global susonly tot_adj_cons ///
                employed ///
                self_emp_bus_owner ///
                hours_worked ///

    local consvar tot_adj_cons
}
global internet_vars use_internet ///
            use_internet_bank
            
global sakonly employed ///
            self_emp_bus_owner ///
            hours_worked ///
            hourly_adj_wage ///
            ind_adj_earnings ///
            looking_job ///
            prep_new_bus

/*----------------------------------------------------*/
* Section: Regression Specifications
/*----------------------------------------------------*/

* SE CLUSTERING (OLS): Individual level
global vce vce(cluster anon_id4)

* INSTRUMENT (IV): win in batch
global instrument_iv win_in_batch

* SE CLUSTERING (IV): Individual level
global cluster cluster(anon_id4)

* FIXED EFFECTS: Strata and inputs into PMO-assigned weight interacted by batch
global absorb absorb(c.stratum_win_prob#batch gender#batch urban#batch ed_cat#batch)

* FIXED EFFECTS (STACKED REGRESSION): Strata, SAKERNAS wave and inputs into PMO-assigned weight interacted by batch and SAKERNAS wave
global absorb_stacked_sak absorb(c.stratum_win_prob#batch#sak_round gender#batch#sak_round urban#batch#sak_round ed_cat#batch#sak_round)

* FIXED EFFECTS (STACKED REGRESSION, interacted with female): Strata, SAKERNAS wave and inputs into PMO-assigned weight interacted by batch and SAKERNAS wave
global absorb_stacked_female_sak absorb(c.stratum_win_prob#batch#sak_round c.stratum_win_prob#female gender#batch#sak_round urban#batch#sak_round ed_cat#batch#sak_round)                      

* FIXED EFFECTS (STACKED REGRESSION, interacted with urban): Strata, SAKERNAS wave and inputs into PMO-assigned weight interacted by batch and SAKERNAS wave
global absorb_stacked_urban_sak absorb(c.stratum_win_prob#batch#sak_round c.stratum_win_prob#urban gender#batch#sak_round urban#batch#sak_round ed_cat#batch#sak_round)                              

* FIXED EFFECTS (STACKED REGRESSION, interacted with java): Strata, SAKERNAS wave and inputs into PMO-assigned weight interacted by batch and SAKERNAS wave
global absorb_stacked_java_sak absorb(c.stratum_win_prob#batch#sak_round c.stratum_win_prob#java gender#batch#sak_round urban#batch#sak_round ed_cat#batch#sak_round) 

* FIXED EFFECTS (STACKED REGRESSION, interacted with abovehs): Strata, SAKERNAS wave and inputs into PMO-assigned weight interacted by batch and SAKERNAS wave
global absorb_stacked_abovehs_sak absorb(c.stratum_win_prob#batch#sak_round c.stratum_win_prob#above_hs gender#batch#sak_round urban#batch#sak_round ed_cat#batch#sak_round)

* FIXED EFFECTS (STACKED REGRESSION, interacted with young): Strata, SAKERNAS wave and inputs into PMO-assigned weight interacted by batch and SAKERNAS wave
global absorb_stacked_young_sak absorb(c.stratum_win_prob#batch#sak_round c.stratum_win_prob#young gender#batch#sak_round urban#batch#sak_round ed_cat#batch#sak_round) 

* FIXED EFFECTS (STACKED REGRESSION, interacted with numapp_xt): Strata, SAKERNAS wave and inputs into PMO-assigned weight interacted by batch and SAKERNAS wave
global absorb_stacked_numapp_sak absorb(c.stratum_win_prob#batch#sak_round c.stratum_win_prob#numapp_xt gender#batch#sak_round urban#batch#sak_round ed_cat#batch#sak_round) 

* FIXED EFFECTS (STACKED REGRESSION, interacted with svygap_a50): Strata, SAKERNAS wave and inputs into PMO-assigned weight interacted by batch and SAKERNAS wave
global absorb_stacked_dur1_sak absorb(c.stratum_win_prob#batch#sak_round c.stratum_win_prob#svygap_a50 gender#batch#sak_round urban#batch#sak_round ed_cat#batch#sak_round) 

* FIXED EFFECTS (STACKED REGRESSION, interacted with svygap_a25): Strata, SAKERNAS wave and inputs into PMO-assigned weight interacted by batch and SAKERNAS wave
global absorb_stacked_dur2_sak absorb(c.stratum_win_prob#batch#sak_round c.stratum_win_prob#svygap_a25 gender#batch#sak_round urban#batch#sak_round ed_cat#batch#sak_round) 

* FIXED EFFECTS (STACKED REGRESSION, interacted with firstapp): Strata, SAKERNAS wave and inputs into PMO-assigned weight interacted by batch and SAKERNAS wave
global absorb_stacked_firstapp_sak absorb(c.stratum_win_prob#batch#sak_round c.stratum_win_prob#firstapp gender#batch#sak_round urban#batch#sak_round ed_cat#batch#sak_round) 

* FIXED EFFECTS (STACKED REGRESSION): Strata, SUSENAS wave and inputs into PMO-assigned weight interacted by batch and SUSENAS wave
global absorb_stacked_sus absorb(c.stratum_win_prob#batch#sus_round gender#batch#sus_round urban#batch#sus_round ed_cat#batch#sus_round)

* FIXED EFFECTS (STACKED REGRESSION, interacted with female): Strata, SUSENAS wave and inputs into PMO-assigned weight interacted by batch and SUSENAS wave
global absorb_stacked_female_sus absorb(c.stratum_win_prob#batch#sus_round c.stratum_win_prob#female gender#batch#sus_round urban#batch#sus_round ed_cat#batch#sus_round)                      

* FIXED EFFECTS (STACKED REGRESSION, interacted with urban): Strata, SUSENAS wave and inputs into PMO-assigned weight interacted by batch and SUSENAS wave
global absorb_stacked_urban_sus absorb(c.stratum_win_prob#batch#sus_round c.stratum_win_prob#urban gender#batch#sus_round urban#batch#sus_round ed_cat#batch#sus_round)                                

* FIXED EFFECTS (STACKED REGRESSION, interacted with java): Strata, SUSENAS wave and inputs into PMO-assigned weight interacted by batch and SUSENAS wave
global absorb_stacked_java_sus absorb(c.stratum_win_prob#batch#sus_round c.stratum_win_prob#java gender#batch#sus_round urban#batch#sus_round ed_cat#batch#sus_round) 

* FIXED EFFECTS (STACKED REGRESSION, interacted with abovehs): Strata, SUSENAS wave and inputs into PMO-assigned weight interacted by batch and SUSENAS wave
global absorb_stacked_abovehs_sus absorb(c.stratum_win_prob#batch#sus_round c.stratum_win_prob#above_hs gender#batch#sus_round urban#batch#sus_round ed_cat#batch#sus_round) 

* FIXED EFFECTS (STACKED REGRESSION, interacted with young): Strata, SUSENAS wave and inputs into PMO-assigned weight interacted by batch and SUSENAS wave
global absorb_stacked_young_sus absorb(c.stratum_win_prob#batch#sus_round c.stratum_win_prob#young gender#batch#sus_round urban#batch#sus_round ed_cat#batch#sus_round) 

* FIXED EFFECTS (STACKED REGRESSION, interacted with numapp_xt): Strata, SUSENAS wave and inputs into PMO-assigned weight interacted by batch and SUSENAS wave
global absorb_stacked_numapp_sus absorb(c.stratum_win_prob#batch#sus_round c.stratum_win_prob#numapp_xt gender#batch#sus_round urban#batch#sus_round ed_cat#batch#sus_round) 

* FIXED EFFECTS (STACKED REGRESSION, interacted with svygap_a50): Strata, SUSENAS wave and inputs into PMO-assigned weight interacted by batch and SUSENAS wave
global absorb_stacked_dur1_sus absorb(c.stratum_win_prob#batch#sus_round c.stratum_win_prob#svygap_a50 gender#batch#sus_round urban#batch#sus_round ed_cat#batch#sus_round) 

* FIXED EFFECTS (STACKED REGRESSION, interacted with svygap_a25): Strata, SUSENAS wave and inputs into PMO-assigned weight interacted by batch and SUSENAS wave
global absorb_stacked_dur2_sus absorb(c.stratum_win_prob#batch#sus_round c.stratum_win_prob#svygap_a25 gender#batch#sus_round urban#batch#sus_round ed_cat#batch#sus_round) 

* FIXED EFFECTS (STACKED REGRESSION, interacted with firstapp): Strata, SUSENAS wave and inputs into PMO-assigned weight interacted by batch and SUSENAS wave
global absorb_stacked_firstapp_sus absorb(c.stratum_win_prob#batch#sus_round c.stratum_win_prob#firstapp gender#batch#sus_round urban#batch#sus_round ed_cat#batch#sus_round) 

/*-----------------------------------------------------*/
* Section: Save survey date-announcment in tempfile
/*-----------------------------------------------------*/

import excel "$KP_deid_admin/Raw/00 Kartu Prakerja Batch Schedule.xlsx", sheet("Batch opening schedule") firstrow clear

replace batch = subinstr(batch, "*", "", .)
destring batch, replace

keep if batch <=22

destring Beneficiariesperbatch, replace
rename Beneficiariesperbatch quota

keep quota batch announcement registrationclose

* Months elapsed the batch announcement date and the survey
gen sak_aug20 = (mdy(8,1,2020)-announcement - 14)/(365/12) if inrange(batch, 2, 3)
gen sak_feb21 = (mdy(2,1,2021)-announcement - 14)/(365/12) if inrange(batch, 2, 11)
gen sak_aug21 = (mdy(8,1,2021)-announcement - 14)/(365/12) if inrange(batch, 2, 17)
gen sus_sep20 = (mdy(9,1,2020)-announcement - 14)/(365/12) if inrange(batch, 2, 5)
gen sus_mar21 = (mdy(3,1,2021)-announcement - 14)/(365/12) if inrange(batch, 2, 11)
gen sus_sep21 = (mdy(9,1,2021)-announcement - 14)/(365/12) if inrange(batch, 2, 18)
gen sus_mar22 = (mdy(3,1,2022)-announcement - 14)/(365/12) if inrange(batch, 2, 22)


tempfile quota
sa `quota', replace

/*----------------------------------------------------------------*/
* Section: Data Setup
/*----------------------------------------------------------------*/

* Load data and append
use "${aug20}", clear
append using "${feb21}"
append using "${aug21}"
append using "${sep20}"
append using "${mar21}" 
append using "${sep21}"
append using "${mar22}"

* Merge in survey-announcement dates
fmerge m:1 batch using `quota', assert(2 3) keep(3) nogen

* Merge in demog match SAK
merge m:1 anon_id4 sak_round using "$KP_deid_sakernas/Clean/age_gender_educ_match_ids.dta", keep(1 3) nogen keepusing(match3_sus)

* Merge in demog match SUS
merge m:1 anon_id4 sus_round using "$KP_deid_susenas/Clean/age_gender_educ_match_ids.dta", keep(1 4) nogen keepusing(match3_sus) update

tab sak_round, m 
tab win_in_batch, m
tab report_applied, m
tab report_selected, m

*Education cat
recode education (1/2 = 1) (3 = 2) (4/8 = 3), gen(ed_cat) 

*Age cat
cap drop age_cat
recode age (0/30 = 1) (31/50 = 2) (51/100 = 3), gen(age_cat) 

mdesc age_cat gender ed_cat multi_apply_hh urban strata

* X VAR: Generate placeholder
gen x = 0
la var x "Treated"

* Hours worked name regularize
replace hours_worked = hrs_worked_all if inlist(sus_round, 6, 8)

*Win before survey var
gen win_before_survey = .
replace win_before_survey  = ever_win_3 if sak_round == 5 & inrange(batch, 2, 3)
replace win_before_survey  = ever_win_11 if sak_round == 6 & inrange(batch, 2, 11)
replace win_before_survey  = ever_win_17 if sak_round == 7 & inrange(batch, 2, 17)
replace win_before_survey  = ever_win_5 if sus_round == 5 & inrange(batch, 2, 5)
replace win_before_survey  = ever_win_11 if sus_round == 6 & inrange(batch, 2, 11)
replace win_before_survey  = ever_win_18 if sus_round == 7 & inrange(batch, 2, 18)
replace win_before_survey  = ever_win_22 if sus_round == 8 & inrange(batch, 2, 22)

* Cash Before Survey
bysort anon_id4: gegen date_incentive = max(date_incentive), replace
gen cash_before_survey =0
replace cash_before_survey  = 1 if sak_round == 5 & inrange(batch, 2, 3) & date_incentive <= date("2020/7/31", "YMD")
replace cash_before_survey  = 1 if sak_round == 6 & inrange(batch, 2, 11) & date_incentive <= date("2021/1/31", "YMD")
replace cash_before_survey  = 1 if sak_round == 7 & inrange(batch, 2, 17) & date_incentive <= date("2021/7/31", "YMD")
replace cash_before_survey  = 1 if sus_round == 5 & inrange(batch, 2, 5) & date_incentive <= date("2020/8/30", "YMD")
replace cash_before_survey  = 1 if sus_round == 6 & inrange(batch, 2, 11) & date_incentive <= date("2021/2/28", "YMD")
replace cash_before_survey  = 1 if sus_round == 7 & inrange(batch, 2, 18) & date_incentive <= date("2021/8/30", "YMD")
replace cash_before_survey  = 1 if sus_round == 8 & inrange(batch, 2, 22) & date_incentive <= date("2022/2/28", "YMD")
gegen cash_before_survey = max(cash_before_survey), by(anon_id4 sus_round sak_round) replace

* HH level win before survey
gen hhid = hh_id_s if !mi(sak_round)
replace hhid = RENUM if inlist(sus_round, 5, 6)
replace hhid = urut if sus_round ==7
replace hhid = URUT if sus_round ==8
bysort hhid batch sak_round sus_round: egen win_before_surveyhh = max(win_before_survey)

* Report Selected SUS
replace report_selected =. if inrange(sus_round, 5, 8)
replace report_selected = hh_pk_win if inlist(sus_round, 6, 7)
replace report_selected = get_pk if inlist(sus_round, 5, 8)

* HH Level Report Selected
bysort hhid batch sak_round sus_round: egen report_selectedhh = max(report_selected)

* Win at first attempt
bysort anon_id4 sak_round sus_round (batch): gen winfirst = win_in_batch ==1 & _n ==1

* Education Dummy
gen above_hs = 1 if inrange(education, 4, 8)
replace above_hs = 0 if inrange(education, 1, 3)

* Win to survey duration dummy
gen svy_getcash_diff = .
replace svy_getcash_diff = sak_aug20 if sak_round ==5
replace svy_getcash_diff = sak_feb21 if sak_round ==6
replace svy_getcash_diff = sak_aug21 if sak_round ==7
replace svy_getcash_diff = sus_sep20 if sus_round ==5
replace svy_getcash_diff = sus_mar21 if sus_round ==6
replace svy_getcash_diff = sus_sep21 if sus_round ==7
replace svy_getcash_diff = sus_mar22 if sus_round ==8

* Susenas: whether time elapsed is greater than 6 months
gen svygap_a50 = svy_getcash_diff > 6 if !mi(sak_round)

* Sakernas: whether time elapsed is greater than 6 months
replace svygap_a50 = svy_getcash_diff > 6 if !mi(sus_round)

* Constant and missing (for missing var regressions)
gen constant = 1
gen missing = .

gen firstapp = batch == first_apply_batch

* Total consumption
gen totcons = tot_adj_cons*hh_size_sus

* HH Earnings for SUS (set equal to total consumption)
* replace hh_adj_earnings = tot_adj_cons*hh_size_sus if inrange(sus_round, 5, 8)

* Hourly Wage for SUS 
* replace hourly_adj_wage = hh_adj_earnings/hh_hrs_worked_all if inrange(sus_round, 6, 8)

* Drop non-randomized batches
drop if inlist(batch, 1, 15)

foreach msuff in "_match"  "" {

    if "`msuff'" =="" local and = ""
    else local and = "& match3_sus ==1"
  
    * SAMPLE RESTRICTIONS: restrict to person-batch observations before date surveyed 
    global precomma_3 inrange(batch, 2, 3) `and'
    global precomma_11 inrange(batch, 2, 11) `and'
    global precomma_17 inrange(batch, 2, 17) `and'

    global precomma_5 inrange(batch, 2, 5) `and'
    global precomma_11 inrange(batch, 2, 11) `and'
    global precomma_18 inrange(batch, 2, 18) `and'
    global precomma_22 inrange(batch, 2, 22) `and'

    global precomma_stack_17 ((sak_round == 5 & inrange(batch, 2, 3)) | (sak_round == 6 & inrange(batch, 2, 11)) | (sak_round == 7 & inrange(batch, 2, 17))) `and'
    global precomma_stack_22 ((sus_round == 5 & inrange(batch, 2, 5)) | (sus_round == 6 & inrange(batch, 2, 11)) | (sus_round == 7 & inrange(batch, 2, 18)) | (sus_round == 8 & inrange(batch, 2, 22))) `and'

    /*----------------------------------------------------------------*/
    * Section: IV Regressions: Main Table
    /*----------------------------------------------------------------*/

    * Declare wave-specific specifications
    global columns iva_sa iva_3 iva_11sa iva_17 iva_su iva_5 iva_11su iva_18 iva_22
    global column_num 9

    * Declare empty model and variable lists
    global model_list
    global var_list

    * Declare variable list for table
    global vars $main_vars

    * loop through each variable of table
    foreach y of varlist $vars {

        * Run IV regression (actual win)
        replace x = win_before_survey

        * Sak Regs
        summ `y' if sak_round == 5
        if r(N) != 0 {
            iv_reg `y'_iva_sa `y' x "$instrument_iv" "" "$precomma_stack_17" "$absorb_stacked_sak" "$cluster" // (batch 2-17)
            iv_reg `y'_iva_3 `y' x "$instrument_iv" "" "$precomma_3 & sak_round ==5" "$absorb" "$cluster" // (batch 2-3)
            iv_reg `y'_iva_11sa `y' x "$instrument_iv" "" "$precomma_11 & sak_round ==6" "$absorb" "$cluster" // (batch 2-11)
            iv_reg `y'_iva_17 `y' x "$instrument_iv" "" "$precomma_17 & sak_round ==7" "$absorb" "$cluster" // (batch 2-17)
        }

        summ `y' if sak_round == 5
        if r(N) == 0 {
            constant_reg `y'_iva_sa // (batch 2-17)
            constant_reg `y'_iva_3 // (batch 2-3)
            constant_reg `y'_iva_11sa // (batch 2-11)
            constant_reg `y'_iva_17 // (batch 2-17)
        }

        *Sus Regs
        summ `y' if sus_round ==6
        if r(N) != 0 {
            *Stacked
            iv_reg `y'_iva_su `y' x "$instrument_iv" "" "$precomma_stack_22" "$absorb_stacked_sus" "$cluster" // (batch 2-22)
            * Mar 21
            iv_reg `y'_iva_11su `y' x "$instrument_iv" "" "$precomma_11 & sus_round ==6" "$absorb" "$cluster" // (batch 2-11)
            * Sep 21
            iv_reg `y'_iva_18 `y' x "$instrument_iv" "" "$precomma_18 & sus_round ==7" "$absorb" "$cluster" // (batch 2-18)
            * Mar 22
            iv_reg `y'_iva_22 `y' x "$instrument_iv" "" "$precomma_22 & sus_round ==8" "$absorb" "$cluster" // (batch 2-22)
            
            * Sep 20
            if !inlist("`y'", "hours_worked") iv_reg `y'_iva_5 `y' x "$instrument_iv" "" "$precomma_5 & sus_round ==5" "$absorb" "$cluster" // (batch 2-5)
            if inlist("`y'", "hours_worked") constant_reg `y'_iva_5
        }
        summ `y' if sus_round ==6
        if r(N) == 0 {
            constant_reg `y'_iva_su // (batch 2-22)
            constant_reg `y'_iva_5 // (batch 2-3)
            constant_reg `y'_iva_11su // (batch 2-11)
            constant_reg `y'_iva_18 // (batch 2-18)
            constant_reg `y'_iva_22 // (batch 2-22)
        }  

    * Add models to list
        global model_list $model_list `y'_iva_sa `y'_iva_3 `y'_iva_11sa `y'_iva_17 `y'_iva_su `y'_iva_5 `y'_iva_11su `y'_iva_18 `y'_iva_22 
        * Add var to list
        global var_list $var_list `y'
    }

    * Flip table and export table
    if "`msuff'" == "_match" local tabname = "TableA11b"
    else local tabname = "Table5" 
    save_sterfile "$KP_output/tables/impacts/sterfiles/`tabname'.ster"       
    flip_table x "$var_list" "$model_list" "$columns" "$column_num"
    export_table "$columns" "$KP_output/tables/impacts/tex/`tabname'" "`consvar'"

    /*----------------------------------------------------------------*/
    * Section: IV Regressions, Report Selected Endogenous Var
    /*----------------------------------------------------------------*/

    * Declare wave-specific specifications
    global columns iva_sa iva_3 iva_11sa iva_17 iva_su iva_5 iva_11su iva_18 iva_22
    global column_num 9

    * Declare empty model and variable lists
    global model_list
    global var_list

    * Declare variable list for table
    global vars $main_vars

    * loop through each variable of table
    foreach y of varlist $vars {

        * Run IV regression (actual win)
        replace x = report_selected

        * Sak Regs
        summ `y' if sak_round == 5
        if r(N) != 0 {
            iv_reg `y'_iva_sa `y' x "$instrument_iv" "" "$precomma_stack_17" "$absorb_stacked_sak" "$cluster" // (batch 2-17)
            iv_reg `y'_iva_3 `y' x "$instrument_iv" "" "$precomma_3 & sak_round ==5" "$absorb" "$cluster" // (batch 2-3)
            iv_reg `y'_iva_11sa `y' x "$instrument_iv" "" "$precomma_11 & sak_round ==6" "$absorb" "$cluster" // (batch 2-11)
            iv_reg `y'_iva_17 `y' x "$instrument_iv" "" "$precomma_17 & sak_round ==7" "$absorb" "$cluster" // (batch 2-17)
        }

        summ `y' if sak_round == 5
        if r(N) == 0 {
            constant_reg `y'_iva_sa // (batch 2-17)
            constant_reg `y'_iva_3 // (batch 2-3)
            constant_reg `y'_iva_11sa // (batch 2-11)
            constant_reg `y'_iva_17 // (batch 2-17)
        }

        *Sus Regs
        summ `y' if sus_round ==6
        if r(N) != 0 {
            *Stacked
            iv_reg `y'_iva_su `y' x "$instrument_iv" "" "$precomma_stack_22" "$absorb_stacked_sus" "$cluster" // (batch 2-22)
            * Mar 21
            iv_reg `y'_iva_11su `y' x "$instrument_iv" "" "$precomma_11 & sus_round ==6" "$absorb" "$cluster" // (batch 2-11)
            * Sep 21
            iv_reg `y'_iva_18 `y' x "$instrument_iv" "" "$precomma_18 & sus_round ==7" "$absorb" "$cluster" // (batch 2-18)
            * Mar 22
            iv_reg `y'_iva_22 `y' x "$instrument_iv" "" "$precomma_22 & sus_round ==8" "$absorb" "$cluster" // (batch 2-22)
            
            * Sep 20
            if !inlist("`y'", "hours_worked") iv_reg `y'_iva_5 `y' x "$instrument_iv" "" "$precomma_5 & sus_round ==5" "$absorb" "$cluster" // (batch 2-5)
            if inlist("`y'", "hours_worked") constant_reg `y'_iva_5
        }
        summ `y' if sus_round ==6
        if r(N) == 0 {
            constant_reg `y'_iva_su // (batch 2-22)
            constant_reg `y'_iva_5 // (batch 2-3)
            constant_reg `y'_iva_11su // (batch 2-11)
            constant_reg `y'_iva_18 // (batch 2-18)
            constant_reg `y'_iva_22 // (batch 2-22)
        }
        
    * Add models to list
        global model_list $model_list `y'_iva_sa `y'_iva_3 `y'_iva_11sa `y'_iva_17 `y'_iva_su `y'_iva_5 `y'_iva_11su `y'_iva_18 `y'_iva_22
        * Add var to list
        global var_list $var_list `y'
    }

    * Flip table and export table
    if "`msuff'" == "_match" local tabname = "TableA11c"
    else local tabname = "TableA20" 
    save_sterfile "$KP_output/tables/impacts/sterfiles/`tabname'.ster"       
    flip_table x "$var_list" "$model_list" "$columns" "$column_num"
    export_table "$columns" "$KP_output/tables/impacts/tex/`tabname'" "`consvar'"
}
    
/*----------------------------------------------------------------*/
* Section: OLS Regressions
/*----------------------------------------------------------------*/

* Declare wave-specific specifications
global columns ols_sa ols_3 ols_11sa ols_17 ols_su ols_5 ols_11su ols_18 ols_22
global column_num 9

* Declare empty model and variable lists
global model_list
global var_list

* Declare variable list for table
global vars $main_vars

* loop through each variable of table
foreach y of varlist $vars {

* Run OLS regression (actual win)
    replace x = win_in_batch
    
* Sak Regs
    summ `y' if sak_round == 5
    if r(N) != 0 {
        ols_reg `y'_ols_sa `y' x "" "$precomma_stack_17" "$absorb_stacked_sak" "$vce" // (batch 2-17)
        ols_reg `y'_ols_3 `y' x "" "$precomma_3 & sak_round ==5" "$absorb" "$vce" // (batch 2-3)
        ols_reg `y'_ols_11sa `y' x "" "$precomma_11 & sak_round ==6" "$absorb" "$vce" // (batch 2-11)
        ols_reg `y'_ols_17 `y' x "" "$precomma_17 & sak_round ==7" "$absorb" "$vce" // (batch 2-17)
    }

    summ `y' if sak_round == 5
    if r(N) == 0 {
            constant_reg `y'_ols_sa // (batch 2-17)
            constant_reg `y'_ols_3 // (batch 2-3)
            constant_reg `y'_ols_11sa // (batch 2-11)
            constant_reg `y'_ols_17 // (batch 2-17)
        }

    *Sus Regs
    summ `y' if sus_round ==6
    if r(N) != 0 {
        *Stacked
        ols_reg `y'_ols_su `y' x "" "$precomma_stack_22" "$absorb_stacked_sus" "$vce" // (batch 2-22)
        *Mar 21
        ols_reg `y'_ols_11su `y' x "" "$precomma_11 & sus_round ==6" "$absorb" "$vce" // (batch 2-11)
        *Sep 21
        ols_reg `y'_ols_18 `y' x "" "$precomma_18 & sus_round ==7" "$absorb" "$vce" // (batch 2-18)
        * Mar 22
        ols_reg `y'_ols_22 `y' x "" "$precomma_22 & sus_round ==8" "$absorb" "$vce" // (batch 2-22)

        *Sep 20
        if !inlist("`y'", "hours_worked")  ols_reg `y'_ols_5 `y' x "" "$precomma_5 & sus_round ==5" "$absorb" "$vce" // (batch 2-5)
        if inlist("`y'", "hours_worked")  constant_reg `y'_ols_5
        
    }
    
    summ `y' if sus_round ==6
    if r(N) == 0 {
            constant_reg `y'_ols_su // (batch 2-22)
            constant_reg `y'_ols_5 // (batch 2-3)
            constant_reg `y'_ols_11su // (batch 2-11)
            constant_reg `y'_ols_18 // (batch 2-18)
            constant_reg `y'_ols_22 // (batch 2-22)
        }

    * Add var to list
    global var_list $var_list `y'
    * Add models to list
    global model_list $model_list `y'_ols_sa `y'_ols_3 `y'_ols_11sa `y'_ols_17 `y'_ols_su `y'_ols_5 `y'_ols_11su `y'_ols_18 `y'_ols_22

}

* Flip table and export table
save_sterfile "$KP_output/tables/impacts/sterfiles/TableA19.ster"       
flip_table x "$var_list" "$model_list" "$columns" "$column_num"
export_table "$columns" "$KP_output/tables/impacts/tex/TableA19" "`consvar'"

/*----------------------------------------------------------------*/
* Section: IV Regressions: HH Won
/*----------------------------------------------------------------*/

* Declare wave-specific specifications
global columns ivasa iva3 iva11sa iva17 ivasu iva5 iva11su iva18 iva22
global column_num 9

* Declare empty model and variable lists
global model_list
global var_list

* Declare variable list for table
global vars $main_vars

* loop through each variable of table
foreach y of varlist $vars {

    * Run IV regression (actual win)
    replace x = win_before_surveyhh

    * Sak Regs
    summ `y' if sak_round == 5
    if r(N) != 0 {
        iv_reg `y'_ivasa `y' x "$instrument_iv" "" "$precomma_stack_17" "$absorb_stacked_sak" "$cluster" // (batch 2-17)
        iv_reg `y'_iva3 `y' x "$instrument_iv" "" "$precomma_3 & sak_round ==5" "$absorb" "$cluster" // (batch 2-3)
        iv_reg `y'_iva11sa `y' x "$instrument_iv" "" "$precomma_11 & sak_round ==6" "$absorb" "$cluster" // (batch 2-11)
        iv_reg `y'_iva17 `y' x "$instrument_iv" "" "$precomma_17 & sak_round ==7" "$absorb" "$cluster" // (batch 2-17)
    }

    summ `y' if sak_round == 5
    if r(N) == 0 {
        constant_reg `y'_ivasa // (batch 2-17)
        constant_reg `y'_iva3 // (batch 2-3)
        constant_reg `y'_iva11sa // (batch 2-11)
        constant_reg `y'_iva17 // (batch 2-17)
    }

    *Sus Regs
    summ `y' if sus_round ==6
    if r(N) != 0 {
        *Stacked
        iv_reg `y'_ivasu `y' x "$instrument_iv" "" "$precomma_stack_22" "$absorb_stacked_sus" "$cluster" // (batch 2-22)
        * Mar 21
        iv_reg `y'_iva11su `y' x "$instrument_iv" "" "$precomma_11 & sus_round ==6" "$absorb" "$cluster" // (batch 2-11)
            * Sep 21
        iv_reg `y'_iva18 `y' x "$instrument_iv" "" "$precomma_18 & sus_round ==7" "$absorb" "$cluster" // (batch 2-18)
        * Mar 22
        iv_reg `y'_iva22 `y' x "$instrument_iv" "" "$precomma_22 & sus_round ==8" "$absorb" "$cluster" // (batch 2-22)
        
        * Sep 20
        if !inlist("`y'", "hours_worked") iv_reg `y'_iva5 `y' x "$instrument_iv" "" "$precomma_5 & sus_round ==5" "$absorb" "$cluster" // (batch 2-5)
        if inlist("`y'", "hours_worked") constant_reg `y'_iva5
    }
    summ `y' if sus_round ==6
    if r(N) == 0 {
        constant_reg `y'_ivasu // (batch 2-22)
        constant_reg `y'_iva5 // (batch 2-3)
        constant_reg `y'_iva11su // (batch 2-11)
        constant_reg `y'_iva18 // (batch 2-18)
        constant_reg `y'_iva22 // (batch 2-22)
    }
    
* Add models to list
    global model_list $model_list `y'_ivasa `y'_iva3 `y'_iva11sa `y'_iva17 `y'_ivasu `y'_iva5 `y'_iva11su `y'_iva18 `y'_iva22
    * Add var to list
    global var_list $var_list `y'
}

* Flip table and export table
save_sterfile "$KP_output/tables/impacts/sterfiles/TableA21.ster"       
flip_table x "$var_list" "$model_list" "$columns" "$column_num"
export_table "$columns" "$KP_output/tables/impacts/tex/TableA21" "`consvar'"

/*----------------------------------------------------------------*/
* Section: IV Regressions: Sakernas Heterogeneity Demog
/*----------------------------------------------------------------*/

* Declare wave-specific specifications
global columns ivf_i0 ivf_i1 ivu_i0 ivu_i1 ivj_i0 ivj_i1 ivhs_i0 ivhs_i1 ivy_i0 ivy_i1
global column_num 10

* Declare empty model and variable lists
global model_list
global var_list

* Declare variable list for table
global vars $sakonly

* loop through each variable of table
foreach y of varlist $vars {

    * Run IV regression (actual win)
    replace x = win_before_survey
    iv_reg_interact `y'_ivf `y' x "$instrument_iv" female "" "$precomma_stack_17" "$absorb_stacked_female_sak" "$cluster" // (batch 2-17)
    iv_reg_interact `y'_ivu `y' x "$instrument_iv" urban "" "$precomma_stack_17" "$absorb_stacked_urban_sak" "$cluster" // (batch 2-17)
    iv_reg_interact `y'_ivj `y' x "$instrument_iv" java "" "$precomma_stack_17" "$absorb_stacked_java_sak" "$cluster" // (batch 2-17)
    iv_reg_interact `y'_ivhs `y' x "$instrument_iv" above_hs "" "$precomma_stack_17" "$absorb_stacked_abovehs_sak" "$cluster" // (batch 2-17)
    iv_reg_interact `y'_ivy `y' x "$instrument_iv" young "" "$precomma_stack_17" "$absorb_stacked_young_sak" "$cluster" // (batch 2-17)

* Add models to list
    global model_list $model_list `y'_ivf_i0 `y'_ivf_i1 `y'_ivu_i0 `y'_ivu_i1 `y'_ivj_i0 `y'_ivj_i1 `y'_ivhs_i0 `y'_ivhs_i1 `y'_ivy_i0 `y'_ivy_i1
    * Add var to list
    global var_list $var_list `y'
}

* Flip table and export table
save_sterfile "$KP_output/tables/impacts/sterfiles/TableA24a.ster"       
flip_table_interact x "$var_list" "$model_list" "$columns" "$column_num"
export_table_interact "$columns" "$KP_output/tables/impacts/tex/TableA24a" "employed"
python:
multicol_tex_rows(
'$KP_output/tables/impacts/tex/TableA24a.tex', '$KP_output/tables/impacts/tex/TableA24a.tex', 6, 30)
end

/*----------------------------------------------------------------*/
* Section: IV Regressions: Susenas Heterogeneity Demog
/*----------------------------------------------------------------*/

* Declare wave-specific specifications
global columns ivf_i0 ivf_i1 ivu_i0 ivu_i1 ivj_i0 ivj_i1 ivhs_i0 ivhs_i1 ivy_i0 ivy_i1
global column_num 10

* Declare empty model and variable lists
global model_list
global var_list

* Declare variable list for table
global vars $susonly

* loop through each variable of table
foreach y of varlist $vars {

    * Run IV regression (actual win)

    replace x = win_before_survey
    iv_reg_interact `y'_ivf `y' x "$instrument_iv" female "" "$precomma_stack_22" "$absorb_stacked_female_sus" "$cluster" // (batch 2-22)
    iv_reg_interact `y'_ivu `y' x "$instrument_iv" urban "" "$precomma_stack_22" "$absorb_stacked_urban_sus" "$cluster" // (batch 2-22)
    iv_reg_interact `y'_ivj `y' x "$instrument_iv" java "" "$precomma_stack_22" "$absorb_stacked_java_sus" "$cluster" // (batch 2-22)
    iv_reg_interact `y'_ivhs `y' x "$instrument_iv" above_hs "" "$precomma_stack_22" "$absorb_stacked_abovehs_sus" "$cluster" // (batch 2-22)
    iv_reg_interact `y'_ivy `y' x "$instrument_iv" young "" "$precomma_stack_22" "$absorb_stacked_young_sus" "$cluster" // (batch 2-22)
    
* Add models to list
    global model_list $model_list `y'_ivf_i0 `y'_ivf_i1 `y'_ivu_i0 `y'_ivu_i1 `y'_ivj_i0 `y'_ivj_i1 `y'_ivhs_i0 `y'_ivhs_i1 `y'_ivy_i0 `y'_ivy_i1
    * Add var to list
    global var_list $var_list `y'
}

* Flip table and export table
save_sterfile "$KP_output/tables/impacts/sterfiles/TableA24b.ster"       
flip_table_interact x "$var_list" "$model_list" "$columns" "$column_num"
export_table_interact_sus "$columns" "$KP_output/tables/impacts/tex/TableA24b" "`consvar'"
python:
multicol_tex_rows(
'$KP_output/tables/impacts/tex/TableA24b.tex', '$KP_output/tables/impacts/tex/TableA24b.tex', 6, 18)
end

/*----------------------------------------------------------------*/
* Section: IV Regressions: Heterogeneity by Survey-Announcement Date
/*----------------------------------------------------------------*/

* Declare wave-specific specifications
global columns sa50_i0 sa50_i1 su50_i0 su50_i1
global column_num 4

* Declare empty model and variable lists
global model_list
global var_list

* Declare variable list for table
global vars $main_vars

* loop through each variable of table
foreach y of varlist $vars {

    * Run IV regression (actual win)
    replace x = win_before_survey
    * Sak Regs
    summ `y' if sak_round == 5
    if r(N) != 0 {
        iv_reg_interact `y'_sa50 `y' x "$instrument_iv" svygap_a50 "" "$precomma_stack_17" "$absorb_stacked_dur1_sak" "$cluster" // (batch 2-17)
    }

    summ `y' if sak_round == 5
    if r(N) == 0 {
        constant_reg_interact `y'_sa50
    }

    * Sus Regs
    summ `y' if sus_round ==6
    if r(N) != 0 {
        iv_reg_interact `y'_su50 `y' x "$instrument_iv" svygap_a50 "" "$precomma_stack_22" "$absorb_stacked_dur1_sus" "$cluster" // (batch 2-22)
    }

    summ `y' if sus_round ==6
    if r(N) == 0 {
        constant_reg_interact `y'_su50
    }
    
* Add models to list
    global model_list $model_list `y'_sa50_i0 `y'_sa50_i1 `y'_su50_i0 `y'_su50_i1
    * Add var to list
    global var_list $var_list `y'
}

* Flip table and export table
save_sterfile "$KP_output/tables/impacts/sterfiles/TableA22.ster"       
flip_table_interact x "$var_list" "$model_list" "$columns" "$column_num"
export_table_interact_sus "$columns" "$KP_output/tables/impacts/tex/TableA22" "`consvar'"
python:
multicol_tex_rows(
'$KP_output/tables/impacts/tex/TableA22.tex', '$KP_output/tables/impacts/tex/TableA22.tex', 6, 34)
end

/*----------------------------------------------------------------*/
* Section: IV Regressions: Heterogeneity by Applications Number
/*----------------------------------------------------------------*/

* Declare wave-specific specifications
global columns sawf_i0 sawf_i1 suwf_i0 suwf_i1
global column_num 4

* Declare empty model and variable lists
global model_list
global var_list

* Declare variable list for table
global vars $main_vars

* loop through each variable of table
foreach y of varlist $vars {
    
    replace x = win_before_survey
    * Sak Regs
    summ `y' if sak_round == 5
    if r(N) != 0 {
        iv_reg_interact `y'_sawf `y' x "$instrument_iv" firstapp "" "$precomma_stack_17" "$absorb_stacked_firstapp_sak" "$cluster" // (batch 2-17)
    }

    summ `y' if sak_round == 5
    if r(N) == 0 {
        constant_reg_interact `y'_sawf
    }

    * Sus Regs
    summ `y' if sus_round ==6
    if r(N) != 0 {
        iv_reg_interact `y'_suwf `y' x "$instrument_iv" firstapp "" "$precomma_stack_22" "$absorb_stacked_firstapp_sus" "$cluster" // (batch 2-22)
    }

    summ `y' if sus_round ==6
    if r(N) == 0 {
        constant_reg_interact `y'_suwf
    }
        
* Add models to list
    global model_list $model_list `y'_sawf_i0 `y'_sawf_i1 `y'_suwf_i0 `y'_suwf_i1
    * Add var to list
    global var_list $var_list `y'
}

* Flip table and export table
save_sterfile "$KP_output/tables/impacts/sterfiles/TableA23.ster"       
flip_table_interact x "$var_list" "$model_list" "$columns" "$column_num"
export_table_interact_sus "$columns" "$KP_output/tables/impacts/tex/TableA23"  "`consvar'"
python:
multicol_tex_rows(
'$KP_output/tables/impacts/tex/TableA23.tex', '$KP_output/tables/impacts/tex/TableA23.tex', 6, 34)
end

/*----------------------------------------------------------------*/
* Section: IV Regressions: Main Table, Cash Before Survey
/*----------------------------------------------------------------*/

* Declare wave-specific specifications
global columns iva_sa iva_3 iva_11sa iva_17 iva_su iva_5 iva_11su iva_18 iva_22
global column_num 9

* Declare empty model and variable lists
global model_list
global var_list

* Declare variable list for table
global vars $main_vars

* loop through each variable of table
foreach y of varlist $vars {

    * Run IV regression (actual win)
    replace x = cash_before_survey

    * Sak Regs
    summ `y' if sak_round == 5
    if r(N) != 0 {
        iv_reg `y'_iva_sa `y' x "$instrument_iv" "" "$precomma_stack_17" "$absorb_stacked_sak" "$cluster" // (batch 2-17)
        iv_reg `y'_iva_3 `y' x "$instrument_iv" "" "$precomma_3 & sak_round ==5" "$absorb" "$cluster" // (batch 2-3)
        iv_reg `y'_iva_11sa `y' x "$instrument_iv" "" "$precomma_11 & sak_round ==6" "$absorb" "$cluster" // (batch 2-11)
        iv_reg `y'_iva_17 `y' x "$instrument_iv" "" "$precomma_17 & sak_round ==7" "$absorb" "$cluster" // (batch 2-17)
    }

    summ `y' if sak_round == 5
    if r(N) == 0 {
        constant_reg `y'_iva_sa // (batch 2-17)
        constant_reg `y'_iva_3 // (batch 2-3)
        constant_reg `y'_iva_11sa // (batch 2-11)
        constant_reg `y'_iva_17 // (batch 2-17)
    }

    *Sus Regs
    summ `y' if sus_round ==6
    if r(N) != 0 {
        *Stacked
        iv_reg `y'_iva_su `y' x "$instrument_iv" "" "$precomma_stack_22" "$absorb_stacked_sus" "$cluster" // (batch 2-22)
        * Mar 21
        iv_reg `y'_iva_11su `y' x "$instrument_iv" "" "$precomma_11 & sus_round ==6" "$absorb" "$cluster" // (batch 2-11)
        * Sep 21
        iv_reg `y'_iva_18 `y' x "$instrument_iv" "" "$precomma_18 & sus_round ==7" "$absorb" "$cluster" // (batch 2-18)
        * Mar 22
        iv_reg `y'_iva_22 `y' x "$instrument_iv" "" "$precomma_22 & sus_round ==8" "$absorb" "$cluster" // (batch 2-22)
        
        * Sep 20
        if !inlist("`y'", "hours_worked") iv_reg `y'_iva_5 `y' x "$instrument_iv" "" "$precomma_5 & sus_round ==5" "$absorb" "$cluster" // (batch 2-5)
        if inlist("`y'", "hours_worked") constant_reg `y'_iva_5
    }
    summ `y' if sus_round ==6
    if r(N) == 0 {
        constant_reg `y'_iva_su // (batch 2-22)
        constant_reg `y'_iva_5 // (batch 2-3)
        constant_reg `y'_iva_11su // (batch 2-11)
        constant_reg `y'_iva_18 // (batch 2-18)
        constant_reg `y'_iva_22 // (batch 2-22)
    }  

* Add models to list
    global model_list $model_list `y'_iva_sa `y'_iva_3 `y'_iva_11sa `y'_iva_17 `y'_iva_su `y'_iva_5 `y'_iva_11su `y'_iva_18 `y'_iva_22 
    * Add var to list
    global var_list $var_list `y'
}

* Flip table and export table
save_sterfile "$KP_output/tables/impacts/sterfiles/TableA16.ster"       
flip_table x "$var_list" "$model_list" "$columns" "$column_num"
export_table "$columns" "$KP_output/tables/impacts/tex/TableA16" "`consvar'"